#ifndef GOOLNN_CANGYAN_FILE_CYFILE
#define GOOLNN_CANGYAN_FILE_CYFILE

#include <QObject>
#include <QImage>

#include "tool/Byte.hpp"
#include "file/Image.hpp"

class CYFile : public QObject{
    Q_OBJECT

    private:
        // 文件头部
        const Byte HEADER[15] = {0xE8, 0x8B, 0x8D, 0xE7, 0x9C, 0xBC, 0xE6, 0xB1, 0x89, 0xE5, 0x8C, 0x96, 0xE7, 0xBB, 0x84};
        // 数据版本
        const Byte VERSION[2] = {0x00, 0x01};

        // 旧数据版本
        const Byte VERSION_0[2] = {0x00, 0x00};

        // 文件路径
        QString filepath;

        // 图像数量
        Byte imagesCount;
        Byte savedCount;

        // 保存时间
        unsigned short year;
        Byte month;
        Byte day;

        Byte hour;
        Byte minute;
        Byte second;

        // 图像数据
        QList<Image> images;

        bool saved;
    
    public:
        CYFile(QString filepath);

        Byte loadFile();
        void saveFile();

        void finalise();

        void addImage(QImage image);
        void addNote(Byte index, Note note);
        void addNote(Byte index, unsigned short x, unsigned short y, QString initialText, QString proofreadingText);

        void removeImage(Byte index);
        void removeNote(Byte page, Byte index);

        void setNoteInitialText(Byte pageIndex, Byte noteIndex, QString text);
        void setNoteProofreadingText(Byte pageIndex, Byte noteIndex, QString text);

        void setSaveYear(unsigned short year);
        void setSaveMonth(Byte month);
        void setSaveDay(Byte day);

        void setSaveHour(Byte hour);
        void setSaveMinute(Byte minute);
        void setSaveSecond(Byte second);

        QString getNoteInitialText(Byte pageIndex, Byte noteIndex);
        QString getNoteProofreadingText(Byte pageIndex, Byte noteIndex);

        Byte getImagesCount();
        Byte getSavedCount();

        QString getSavedDate();

        Image getImage(Byte index);

        QString getFileName();
        QString getFilePath();

        bool isSaved();

    private:
        Byte loadOldFile(Byte* version, QDataStream* stream);

        bool dataCompare(Byte* data1, Byte* data2, unsigned int size);

    signals:
        void fileSaved();
        void fileChanged();

};

#endif
